package com.dm.webCrawler.framework.auth;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.dm.webCrawler.framework.auth.mode.LoginUser;
import com.dm.webCrawler.framework.web.utils.ApplicationUtil;
import com.dm.webCrawler.common.exception.CustomException;

public class AuthUtils {

	private static String USER_ATTR_NAME="CURR_USER";
	
	private static AuthCacheUtil getCache() {
		return ApplicationUtil.getBean(AuthCacheUtil.class);
	}
	
	public static LoginUser getCacheUser(HttpServletRequest request) {
		return getCache().getUserBytoken(request.getHeader("Authorization"), true);
	}
	
	
	public static void setCurrUser(HttpSession session,LoginUser user) {
		session.setAttribute(USER_ATTR_NAME, user);
	}
	public static LoginUser getCurrUser(HttpSession session) {
		Object userObj=session.getAttribute(USER_ATTR_NAME);
		if(userObj!=null && userObj instanceof LoginUser) {
			return (LoginUser)userObj;
		}else {
			throw new CustomException("当前请求无法获取用户信息！");
		}
	}
	
}
